function  yf=filtk(y,a,n);

% filk.m: function file for filtering data.  
% It is designed assuming that that the data is 
% organized (rows=obs, columns=series).  There is
% a vector of filter coefficients.  There are two
% possible structures for the filter weights.  
% The default assumption is that the filter is 
% symmetric with weights given in the vector
% a=[a0 a1 ... aK]: since there is symmetry a(-j)
% = a(j) and we do not repeat the coeeficients.
% The alternative is that there is a one sided (lag)
% filter with weights a=[a0 a1 ... aK].

if (nargin<3)
   n=2;  % default is two sided filter.
end

K=max(size(a))-1;  % max lag;

T=max(size(y));    % number of observations;

% One-sided filter, 

if (n==1)
   
flip=K+1:-1:1;
avec=a(flip);
 
yf=zeros(size(y));

for t=K+1:1:T;
 yf(t,:)=avec*y(t-K:t,:);
end

elseif (n==2)
   
% Set vector of weights

avec=zeros(1,2*K+1);
avec(K+1)=a(1);
for i=1:K;
avec(K+1-i)=a(i+1);
avec(K+1+i)=a(i+1);
end
yf=zeros(size(y));
for t=K+1:1:T-K
 yf(t,:)=avec*y(t-K:t+K,:);
end

else
   disp('filtk.m error: third argument must be:')
   disp('  1 for one-sided (lag) filter')
   disp('  2 for two sided filter')
end
